﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRM.Database.Model.Users>" %>
<%@ Import Namespace="CRM.Database.Model" %>
<%@ Import Namespace="CRM.Resourses" %>

<% Html.DevExpress().GridView(settings =>
                                    {
                                        settings.Name = "gdRoles";
                                        settings.KeyFieldName = "IdUsersInRole";
                                        //var firstOrDefault = Model.FirstOrDefault();
                                        //if (firstOrDefault != null)
                                            settings.CallbackRouteValues =
                                                new { Controller = "Users", Action = "RolesPartial", id=Model.IdUser };
                                        settings.SettingsPager.PageSize = 20;

                                        settings.Columns.Add(column =>
                                        {
                                            column.FieldName = "IdRole";
                                            column.Caption = Resourses.Role;
                                            column.ColumnType = MVCxGridViewColumnType.ComboBox;
                                            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                                            if (comboBoxProperties != null)
                                            {
                                                var col = new RolesCollection();
                                                comboBoxProperties.DataSource = col.RolesList;
                                                comboBoxProperties.TextField = "RoleNameLocalized";
                                                comboBoxProperties.ValueField = "IdRole";
                                                comboBoxProperties.ValueType = typeof(int);
                                            }
                                        });
                                        //
                                        //Добавление редактирования
                                        //
                                        settings.CommandColumn.Visible = true;
                                        settings.CommandColumn.NewButton.Visible = true;
                                        settings.CommandColumn.DeleteButton.Visible = true;
                                        settings.CommandColumn.EditButton.Visible = true;
                                        settings.SettingsBehavior.ConfirmDelete = true;
                                        
                                     //   var usersInRoles = Model.FirstOrDefault();
                                        //if (usersInRoles != null)
                                        //{
                                        settings.SettingsEditing.AddNewRowRouteValues =
                                            new
                                                {
                                                    Controller = "Users",
                                                    Action = "UsersInRolesPartialAdd", id=Model.IdUser
                                                };
                                            settings.SettingsEditing.UpdateRowRouteValues =
                                                new { Controller = "Users", Action = "UsersInRolesPartialUpdate", id = Model.IdUser };
                                        //}
                                       
                                            settings.SettingsEditing.DeleteRowRouteValues =
                                                new
                                                    {
                                                        Controller = "Users",
                                                        Action = "UsersInRolesPartialDelete",
                                                        id = Model.IdUser
                                                    };

                                        settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
                                        settings.Width = Unit.Percentage(100);
                                    }).Bind(Model.UsersInRoles.ToList()).GetHtml();%>

